﻿// See https://aka.ms/new-console-template for more information
using Config1;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

//直接命令行启动传参(比如docker,本地linux中需要传命令行参数),进入到bin目录:cmd -->执行命令:dotnet config1.dll name=hello age=5
ConfigurationBuilder builder = new ConfigurationBuilder();
//命令行参数
//builder.AddCommandLine(args); //这里顶级语句可以也是可以直接获取main函数的args参数的

//环境变量
//builder.AddEnvironmentVariables();

//环境变量,自定义前缀,在环境变量中也加上前缀C1_,为了不和操作系统的环境变量冲突
builder.AddEnvironmentVariables("C1_");

IConfigurationRoot configRoot =builder.Build();

//Console.WriteLine($"name:{configRoot["name"]},age:{configRoot["age"]}");
//Console.ReadKey();



ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<TestController>();
serviceCollection.AddScoped<Test2>();
//把Config对象绑定到根节点上去
//serviceCollection.AddOptions().Configure<Config>(a=> configRoot.Bind(a));
//把Proxy对象绑定到proxy节点上去
//serviceCollection.Configure<Proxy>(a=>configRoot.GetSection("proxy").Bind(a));

//可以链式配置
serviceCollection
    .Configure<Config>(a => configRoot.Bind(a))
    .Configure<Proxy>(a => configRoot.GetSection("proxy").Bind(a));

using (ServiceProvider sp = serviceCollection.BuildServiceProvider())
{
    while (true)
    {
        using (IServiceScope scope = sp.CreateScope())
        {
            var c = scope.ServiceProvider.GetRequiredService<TestController>();
            c.Test();

            var c2 = scope.ServiceProvider.GetRequiredService<Test2>();
            c2.Test();
        }

        Console.WriteLine("点击任意键继续");
        Console.ReadKey();
    }

}








